//
//  PitchNewItemViewController.m
//  KG
//
//  Created by Eugene Braginets on 06.05.12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "PitchNewItemViewController.h"
#import "showOnMapViewController.h"
#import "AppDelegate.h"
#import "Place.h"
#import "bannerView.h"

@implementation PitchNewItemViewController
@synthesize place=_place;
@synthesize address = _address;
@synthesize pitchTitle = _pitchTitle;
@synthesize placeID=_placeID;
@synthesize flag = _flag;
@synthesize pitchDescriptionText = _pitchDescriptionText;
@synthesize displayOnMap = _displayOnMap;
@synthesize pitchImage = _pitchImage;
@synthesize bannerViewController=_bannerViewController;
@synthesize parsePlaceName=_parsePlaceName;

- (IBAction)showOnMap:(id)sender {
    showOnMapViewController* vc=[[showOnMapViewController alloc] initWithPlace:self.place];
    [self.navigationController pushViewController:vc animated:YES];
    
}

- (id)initWithPlace:(Place *)place {
    self = [super initWithNibName:@"PitchNewItemViewController" bundle:nil];
    if (self) {
        
        NSLog(@"Place: %@",place.placeID);
        self.placeID=place.placeID;
        self.place=place;
        NSString* url=[NSString stringWithFormat:@"%@&item=%@",kGetPitchesURL,self.placeID];
        NSString* name=[NSString stringWithFormat:@"%@%@",kPitchItem,self.placeID];
        self.parsePlaceName=name;
        [myAppDelegate getData:url andType:name];
        self.navigationItem.title=place.title;
        

        NSLog(@"name: %@",name);
        
        
        [[NSNotificationCenter defaultCenter] addObserver:self 
                                                 selector: @selector(refresh:)
                                                     name:name 
                                                   object: nil];
//        
        // Custom initialization
        
    }
    return self;
}




- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}


#pragma mark - View lifecycle

- (void)viewDidLoad
{
    self.pitchTitle.text=self.place.title;
   
   
    
    [self.pitchImage setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:self.place.imageAddr]]]];
     NSLog(@"address: %@",self.place.address);
    bannerView *bannerVC=[[bannerView alloc] init];
    [self.view addSubview:bannerVC.view];
    [bannerVC animate];
    self.bannerViewController=bannerVC;
    
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

#pragma mark - custom methods
- (void) refreshUI {
    self.place=[[kDataArrayStore objectForKey:self.parsePlaceName] objectAtIndex:0];
//    [self.pitchImage setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:self.place.imageAddr]]]];
    [self.flag setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:self.place.flag]]]];
    [self.pitchDescriptionText setText:self.place.text];
    [self.address setText:self.place.address];
    [self.pitchDescriptionText setText:self.place.text];
}

- (void) refresh:(NSNotification*)object {
    NSLog(@"refresh! %@",self.place.placeID);
    if ([kDataArrayStore objectForKey:self.parsePlaceName]) {
        [self performSelectorOnMainThread:@selector(refreshUI) withObject:nil waitUntilDone:false];
    }
}


- (void)viewDidUnload
{
    [self setPitchTitle:nil];
    [self setPitchImage:nil];
    [self setDisplayOnMap:nil];
    [self setFlag:nil];
    [self setPitchDescriptionText:nil];
    [self setAddress:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    NSLog(@"Pitch new item view controller");
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
